import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { fileURLToPath, URL } from 'url';
import AutoImport from "unplugin-auto-import/vite";
import vueSetupExtend from 'vite-plugin-vue-setup-extend';
import path, { resolve } from "path";
const host = process.env.TAURI_DEV_HOST;

// https://vitejs.dev/config/
export default defineConfig(async () => ({
  base: './', // 确保相对路径生效
  plugins: [
    vue({
      template: {
        compilerOptions: {
          // 处理所有以 mso- 开头的标签
          isCustomElement: (tag) => tag.startsWith('mso-')
        }
      }
    }),
    AutoImport({
      imports: ['vue', 'vue-router'],
      defaultExportByFilename: true,
      dts: 'types/auto-imports.d.ts', // 使用typescript，需要指定生成对应的d.ts文件或者设置为true,生成默认导入d.ts文件
      dirs: ['src/stores']
    }),
    vueSetupExtend()
  ],
  build: {
    rollupOptions: {
      input: {
        main: path.resolve(__dirname, 'index.html'),
        frontend: path.resolve(__dirname, 'frontend.html'),
        transition: path.resolve(__dirname, 'transition.html'),
        score:path.resolve(__dirname,'score.html'),
        map:path.resolve(__dirname,'map.html')
      }
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js' // 加入这行
    }
  },
  css: {
    preprocessorOptions: {
      // 全局样式引入
      scss: {
        additionalData: '@use "@/assets/styles/index.scss" as *;',
        javascriptEnabled: true
      }
    }
  },
  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  //
  // 1. prevent vite from obscuring rust errors
  clearScreen: false,
  // 2. tauri expects a fixed port, fail if that port is not available
  server: {
    port: 1420,
    strictPort: true,
    host: host || false,
    hmr: host
      ? {
        protocol: "ws",
        host,
        port: 1421,
      }
      : undefined,
    watch: {
      // 3. tell vite to ignore watching `src-tauri`
      ignored: ["**/src-tauri/**"],
    },
  },
}));
